package com.recSys.dataPrediction;

import com.recSys.dataProcessing.Deal4RecFun;
import com.recSys.dataTraining.Train4RecFun;
import com.recSys.model.BusiProcBaseInfo;
import com.recSys.model.User;
import com.recSys.util.ResourcePathHandler;
import de.bwaldvogel.liblinear.Feature;
import de.bwaldvogel.liblinear.FeatureNode;
import de.bwaldvogel.liblinear.Linear;
import de.bwaldvogel.liblinear.Model;

/**
 * Created by dell on 2018/1/29.
 */
public class Predict4Fun {
    static final String rootPath = ResourcePathHandler.getProgrameRootPath();
    static final String filePath4Fun_model = rootPath + "\\data\\model\\recFun_model.txt";

    public static double predict (User user, BusiProcBaseInfo bpbi) {
        try {
            String cPos = user.getIDCard().substring(0, 6);
            String dPos = bpbi.getDeadIDNumber().substring(0, 6);
            String populace = bpbi.getDeadPopulace();
//            Deal4RecFun.statistic();
            Feature[] instance = new Feature[3];
            if (Deal4RecFun.cPosSet.contains(cPos)) {
                int cPosIndex = Deal4RecFun.cPosSet.headSet(cPos).size() + 1;
                instance[0] = new FeatureNode(cPosIndex, 1);
            }
            if (Deal4RecFun.dPosSet.contains(dPos)) {
                int dPosIndex = Deal4RecFun.cPosSet.size() + Deal4RecFun.dPosSet.headSet(dPos).size() + 1;
                instance[1] = new FeatureNode(dPosIndex, 1);
            }
            if (populace != null && Deal4RecFun.populaceSet.contains(populace)) {
                int populaceIndex = Deal4RecFun.cPosSet.size() + Deal4RecFun.dPosSet.size() + Deal4RecFun.populaceSet.headSet(populace).size() + 1;
                instance[2] = new FeatureNode(populaceIndex, 1);
            }
            Model model = Train4RecFun.modelForRecFun;
            return Linear.predict(model, instance);
        } catch (Exception e) {
            System.out.println(e.getStackTrace());
            return -1.0;
        }
    }
}
